package com.leetcode;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode
 * @Author: lei
 * @CreateTime: 2024-03-29  13:52
 * @Description: TODO
 * @Version: 1.0
 */
public class 元素和最小的山形三元组_1 {
    public static void main(String[] args) {
        //int[] nums = {8, 6, 1, 5, 3};
        int[] nums = {5, 4, 8, 7, 10, 2};
        System.out.println(minimumSum(nums));

    }

    public static int minimumSum(int[] nums) {
        int result = -1;
        for (int i = 1; i < nums.length; i++) {
            int left = 0, right = nums.length - 1;
            int leftNum = nums[left];
            int current = nums[i];
            int rightNum = nums[right];
            while (left < i) {
                leftNum = Math.min(leftNum, nums[left]);
                left++;
            }
            while (i < right) {
                rightNum = Math.min(rightNum, nums[right]);
                right--;
            }
            if (leftNum < current && current > rightNum) {
                result = result == -1 ? leftNum + current + rightNum : Math.min(result, leftNum + current + rightNum);
            }
        }
        return result;

    }
}
